package com.vhall.component.service.room.event.handler.record;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.room.RecordOfRoomMapper;
import com.vhall.component.dao.room.RoomMapper;
import com.vhall.component.entity.record.RecordEntity;
import com.vhall.component.entity.room.entity.RoomsEntity;
import com.vhall.component.plugin.common.constant.HttpConstant;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.room.concant.RoomConstant;
import com.vhall.component.service.room.event.handler.AbstractEventHandler;
import com.vhall.component.service.room.event.handler.EventHandlerType;
import com.vhall.component.service.room.event.handler.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

import static com.vhall.component.service.room.event.EventConstant.CREATE_RECORD_COMPLETE;


/**
 * 生成回放完成 处理器
 *
 * @author jingwen.li
 * @date 2023/2/16
 */
@Slf4j
@Component
@EventHandlerType(CREATE_RECORD_COMPLETE)
public class CreateRecordCompleteEventHandler extends AbstractEventHandler {

    @Autowired
    private RedirectUtil redirectUtil;
    @Value("${old.callback.record.url}")
    private String callbackUrl;
    @Autowired
    private PaasService paasService;
    @Autowired
    private RecordOfRoomMapper recordOfRoomMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public void handle(Map<String, Object> callBack) {
//        redirectUtil.redirect(callbackUrl, callBack);
        log.info("收到生成回放完成事件：{}, 事件详情：{}", CREATE_RECORD_COMPLETE, callBack);
        recordComplete(callBack);
    }

    private void recordComplete(Map<String, Object> call) {
        int status = Integer.parseInt(String.valueOf(call.get("status")));
        String roomId = (String) call.get("room_id");
        if (1 == status) {
            String vodId = (String) call.get(RoomConstant.VOD_ID);
            JSONObject vodInfo = paasService.getRecordInfo(vodId);
            if (vodInfo.getInteger(HttpConstant.CODE) != HttpConstant.SUCCESS_CODE) {
                log.error("获取视频详细信息错误");
            } else {
                RecordEntity recordEntity = recordOfRoomMapper.findByVodId(vodId);
                if (recordEntity != null) {
                    JSONObject data = vodInfo.getJSONObject(HttpConstant.DATA);
                    JSONObject vod = data.getJSONObject("vod_info");
                    recordEntity.setStorage(vod.getInteger("storage"));
                    recordEntity.setDuration(vod.getInteger("duration"));
                    recordEntity.setUpdatedAt(LocalDateTime.now());
                    if (checkLiveRormal(roomId)){
                        // 测试房间直播回放 默认通过审核
                        recordEntity.setAuditStatus(1);
                    }
                    recordOfRoomMapper.updateById(recordEntity);
                }
            }
        } else {
            log.error("房间生成回放失败：{}", roomId);
        }
    }

    private RoomsEntity getRoom(String roomId){
        LambdaQueryWrapper<RoomsEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RoomsEntity::getRoomId, roomId);
        queryWrapper.last(" limit 1");
        return  roomMapper.selectOne(queryWrapper);
    }

    private boolean checkLiveRormal(String roomId){
        RoomsEntity room = getRoom(roomId);
        return room.getLiveRormal() == 0;
    }
}
